var util = require('../../libs/util.js');
var app = getApp();
var ringChart = null;
Page({
  data: {
    userInfo: {},
    truename: "",
    hasData: false,
    holiday_start: "",
    holiday_end: "",
    replace_driver: "选择替班司机",
    replace_driver_id: ''
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--请假申请" });

    that.setData({
      userInfo: app.globalData.userInfo,
      truename: app.globalData.truename,
      card: e.card
    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 100,
          holiday_start: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
          holiday_end: util.dateObjectformat(new Date(), 'yyyy-MM-dd')
        });
      }
    });

    wx.showLoading({
      title: '请稍候...',
    });
    util.getData("driver", { openid: app.globalData.openid }, function (res) {
      console.log(res.data)
      if (res.data.success) {
        var data = res.data.data, driver_name = [], driver_id = [], driver_index = [];
        for (var i = 0; i < data.length; i++) {
          driver_name.push(data[i].name);
          driver_id.push(data[i].ID);
          driver_index.push(i);
        }
        that.setData({
          driver_name: driver_name,
          driver_id: driver_id,
          driver_index: driver_index
        });
      }
      wx.hideLoading();
    });
  },
  fromSubmit: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    if (new Date(that.data.holiday_end) < new Date(that.data.holiday_start)) {
      wx.showToast({
        title: "结束时间错误",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    var data = {
      holiday_start: that.data.holiday_start,
      holiday_end: that.data.holiday_end,
      holiday_reason: that.data.holiday_reason,
      replace_driver: that.data.replace_driver_id,
      vehicle: app.globalData.vehicle,
      user_id: app.globalData.user_id,
      co_id: app.globalData.co_id,
      appTpl: '1B0C2579B56740699C07D0ECB036C4A1',
      FORM_NO: 133,
      RES_ID: 268,
      formCName: '请假管理',
      flowID: 62,
      formID: '',
      draft: false
    };
    for (var n in data) {
      if (n != 'card' && data[n] == undefined) {
        wx.showToast({
          title: "请填写所有项",
          image: '/images/wrong.png',
          duration: 2000
        });
        return false;
      }
    }
    util.postData("/form/postData", data, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showToast({
          title: res.data.data,
          icon: 'success',
          duration: 3000,
          mask: true,
          success: function () {
            wx.redirectTo({
              url: '/pages/driver/applyHoliday'
            });
          }
        });
      } else if (res.data.success == false) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  bindHolidayEndChange: function (e) {
    this.setData({
      holiday_end: e.detail.value
    })
  },
  bindHolidayStartChange: function (e) {
    this.setData({
      holiday_start: e.detail.value
    })
  },
  bindReplaceDriverChange: function (e) {
    this.setData({
      replace_driver: this.data.driver_name[e.detail.value],
      replace_driver_id: this.data.driver_id[e.detail.value]
    })
  },
  gotoDriverHome: function () {
    wx.redirectTo({
      url: '/pages/driver/orderList'
    })
  },
  gotoDriverInfo: function () {
    wx.redirectTo({
      url: '/pages/driver/driverInfo'
    })
  },
  gotoCurrentOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/currentOrder'
    })
  },
  gotoHistoryOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/historyOrder'
    })
  }
})
